package com.dyrj.question.service.impl;

import com.dyrj.question.bean.*;
import com.dyrj.question.dao.*;
import com.dyrj.question.service.IPDayawardService;
import com.dyrj.utils.DateUtil;
import com.dyrj.utils.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.dyrj.base.system.user.model.SysUser;
import com.dyrj.component.properties.UploadProperties;
import com.dyrj.constant.SysFrameConst;
import com.dyrj.utils.UUIDUtil;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 日抽奖管理 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-04-30
 */
@Service
@Log4j2
public class PDayawardServiceImpl implements IPDayawardService {

    @Resource
    private PDayawardMapper mapper;
    @Resource
    private PPrizeruleMapper pPrizeruleMapper;
    @Resource
    private PDayanswerMapper pDayanswerMapper;
    @Resource
    private PDayexchangeMapper pDayexchangeMapper;
    @Resource
    private PShopingMapper pShopingMapper;

    @Resource
    private UploadProperties uploadProperties;
    @Value("${string.maxprize}")
    private BigDecimal maxprize;
    @Value("${string.minprize}")
    private BigDecimal minprize;
    @Value("${string.prizerate}")
    private String prizerate;
    @Override
    public PageResultMsg table(PDayaward record) {
        PageInfo<PDayaward> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.selectList(record));
        return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
    }


    @Override
    public ResultMsg add(PDayaward record) {
        String msg = "";
        try {
            mapper.insertSelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE+msg);
    }


    @Override
    public PDayaward info(String id) {
        PDayaward ylMmdagl = mapper.selectByPrimaryKey(Integer.parseInt(id));
        return ylMmdagl;
    }

    public ResultMsg update(PDayaward record) {
        String msg = "";
        try {
            mapper.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE + msg);
    }

    @Override
    public ResultMsg delete(String id) {
        mapper.deleteByPrimaryKey(Integer.parseInt(id));
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
    }

    //获取奖金
    public double getAward(String uid){
        // 获取当日抽奖累计数，不能大于300元，同时不能小于5云
        PDayaward pDayaward = new PDayaward();
        pDayaward.setRq(DateUtil.format(DateUtil.getDate(),"yyyy-MM-dd"));
        List<PDayaward> pDayawardList =  mapper.selectList(pDayaward);
        BigDecimal totalAward=new BigDecimal(0);
        for(int i=0;i<pDayawardList.size();i++){
            if("自动发放代金卷".equals(pDayawardList.get(i).getBz())) continue;
            totalAward = totalAward.add(pDayawardList.get(i).getAward());
        }
//        System.out.println(this.maxprize+"==================="+totalAward+"==================="+this.maxprize.subtract(totalAward).doubleValue());
        if(this.maxprize.subtract(totalAward).doubleValue()>5) {
            List pDayanswerlist = pDayanswerMapper.getPointList(uid, "",this.prizerate);

            PPrizerule pPrizerule = new PPrizerule();
            pPrizerule.setNd("2021");
            if (pDayanswerlist.size() == 1) {
                pPrizerule.setAnswercn("第1次答题全对");
            } else if (pDayanswerlist.size() == 2) {
                pPrizerule.setAnswercn("第2次答题全对");
            } else if (pDayanswerlist.size() > 2) {
                pPrizerule.setAnswercn("第3次以及以后答题全对");
            }
            List<PPrizerule> list = pPrizeruleMapper.selectList(pPrizerule);
            if (list.size()>0) {
                PPrizerule pPrizerule1 = list.get(0);
                String prize = pPrizerule1.getPrize();
                String prizeArr[] = StringUtil.split(prize, "-");
                double money = getrandom(Double.parseDouble(prizeArr[0]), Double.parseDouble(prizeArr[1]));
                return money;
            }else{
                return 0;
            }
        }
        return 0;
    }

    public static double getrandom(Double start,Double end) {
        String retVal=null;
        double num=(Math.random()*(end-start+1)+start);
        if(num>end){
            retVal=(int)num+"";
        }else{
            retVal=format1(num,"0.0");
        }
        Double retVal1= Double.parseDouble(retVal);
        if(retVal1>end){
            return end;
        }else if(retVal1==0){
            return start;
        }else{
            return retVal1;
        }
    }
    public static String format1(Double source,String pattern){
        NumberFormat nf = new DecimalFormat(pattern);
        String result = nf.format(source);
        return result;
    }
    public static void main(String args[]){
        for(int i=0;i<100;i++) {
            double num = getrandom(0.05,0.1);
            System.out.println(num);
        }
    }
    public BigDecimal getTotalAward(String uid){
        BigDecimal retNum=null;
        PDayaward pDayaward = new PDayaward();
        pDayaward.setUid(uid);
        List<PDayaward> pDayawardList =  mapper.selectList(pDayaward);
        BigDecimal totalAward=new BigDecimal(0);
//        BigDecimal result1 = num1.add(num2);
        for(int i=0;i<pDayawardList.size();i++){
            totalAward = totalAward.add(pDayawardList.get(i).getAward());
        }

        PDayexchange pDayexchange = new PDayexchange();
        pDayexchange.setUid(uid);
        List<PDayexchange> pDayexchangeList = pDayexchangeMapper.selectList(pDayexchange);
        BigDecimal totalAward1=new BigDecimal(0);
        for(int i=0;i<pDayexchangeList.size();i++){
            PShoping pShoping =  pShopingMapper.selectByPrimaryKey(pDayexchangeList.get(i).getShopid());
            if(pShoping!=null) {
                totalAward1 = totalAward1.add(pShoping.getPrice());
            }
        }

        retNum = totalAward.subtract(totalAward1);
//        System.out.println(totalAward+"===="+totalAward1+"===="+retNum);
        return retNum;
    }
}
